import { Component, OnInit } from '@angular/core'
import { JsonApiService } from 'app/core/api/json-api.service' //http请求配置引入
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { config } from '../../shared/smartadmin.config'

declare var $: any
@Component({
  selector: 'sa-calibration-result',
  templateUrl: './calibration-result.component.html'
})
export class CalibrationResultComponent implements OnInit {
  tableData: any[] = [] //table数据
  totalCount: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  searchKey: string = ''
  deviceId: string = ''
  carNumber: string = ''
  selectedCompanyId: string = ''
  selectedCompanyName: string = ''
  startTime: string = '' //table时间控件
  endTime: string = '' //table时间控件
  initSelectDay = 3 // 时间选择框默认查询时间跨度（day）
  appType: string = ''
  calibrationResult: any = '' //标定结果 0表示成功，1表示失败

  specifyTime = {
    startTime: '',
    endTime: ''
  }
  activeSelected: string = '' //当前选择的row数据

  carTypeEum = ['侧挂后挂+接近开关', '后挂高低位车+倾角', '后挂低位车+倾角', '后挂高位车+倾角', '尾板+油压', '尾板+秤台', '整车+贴片', '后挂高低位车+双倾角', '660+倾角', '接近开关', '倾角+后挂', '倾角+高位拉杆', '倾角+高低位拉杆', '240II']
  constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
    activatedRoute.queryParams.subscribe(queryParams => {
      this.curPage = queryParams.nowPage || 1
    })
  }
  //时间change回调
  getSelectTableTime(val) {
    this.startTime = val.startTime
    this.endTime = val.endTime
    // 初始化时间后会触发这个回调，所以在这里调用 load 方法
    this.load()
  }
  ngOnInit() {
    this.load()
  }
  
  //table刷新
  refresh() {
    this.load()
  }
  //表格搜索
  table_search() {
    this.curPage = 1
    this.load()
  }
  //初始化加载list数据
  load() {
    let timeCheck = common.correctTimeParams(this.startTime, this.endTime, 0)
    if (!timeCheck.status) {
      this.jsonApiService.message(timeCheck.message)
      return
    }
    const data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        companyId: this.selectedCompanyId,
        companyName: this.selectedCompanyName,
        deviceId: this.deviceId,
        carNumber: this.carNumber,
        appType: this.appType,
        calibrationResult: this.calibrationResult,
        // startTime: this.startTime,
        // endTime: this.endTime,
        searchKey: common.stringTrim(this.searchKey)
      }
    }
    const reqdata = 'gridPager=' + JSON.stringify(data)
    this.jsonApiService.http('sanitationCalibrationInfo/sanitationCalibrationLogList.html', reqdata).subscribe(data => {
      if (data.isSuccess) {
        this.tableData = data.exhibitDatas
        this.totalCount = data.recordCount
      } else {
        this.jsonApiService.message(data.message)
      }
    })
  }
  // 标定删除
  delRow(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除?').then(() => {
      this.jsonApiService.http('sanitationCalibrationInfo/deleteSanitationCalibrationRecords.html', 'id=' + row.id + '&deleteFlag=log').subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  //分页回调
  paginate(e) {
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  //公司选中回调
  companySelected(data) {
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
    this.deviceId = data.deviceId
    this.carNumber = data.carNumber
  }
  // 数据导出
  exportData() {
    const data = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        companyId: this.selectedCompanyId,
        companyName: this.selectedCompanyName,
        deviceId: this.deviceId,
        carNumber: this.carNumber,
        appType: this.appType,
        calibrationResult: this.calibrationResult,
        searchKey: common.stringTrim(this.searchKey),
        export: '1'
      }
    }
    common.exprotDataFile(data, 'gridPager', config.BaseURL + 'sanitationCalibrationInfo/sanitationCalibrationLogList.html')
  }

  downloadFile(row) {
    if(!row.fileUrl) {
      this.jsonApiService.message('无下载地址')
      return
    }
		// let _url=config.BaseURL + row.fileUrl
		window.open(row.fileUrl);
  }
}
